package com.darwin.darwinDisplay
{
	import com.darwin.darwinBitmapEngine.core.MovieArrayNode;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shader;
	import flash.display.Shape;
	import flash.events.TimerEvent;
	import flash.filters.BitmapFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * darx动画绘制完成时抛出此事件 
	 */
	[Event(name="darx_draw_complete",type="com.darwin.darwinDisplay.DarXBitmapMovieNodeEvent")]
	/**
	 *	darx动画播放完成事件  
	 */	
	[Event(name="darx_play_complete",type="com.darwin.darwinDisplay.DarXBitmapMovieNodeEvent")]

	/**
	 *	将多个深度贴图动画(DarXBitmapData)组合成一个完整动画列表进行缓存和播放的动画类
	 * </br>多用于纸娃娃等组合系统
	 * @author Roland
	 * 
	 */
	public class DarXBitmapAvatarNode extends MovieArrayNode
	{
		/**
		 *	目前最大支持的输入源(DarXBitmapData)的个数 
		 */
		private static const MAX_INPUT_NUM:int = 4;
		
		private var _darxBitmapDataList:Vector.<DarXBitmapData>;

		private var _darxBitmapDataListChanged:Boolean = false;
		
		private var _filtersList:Vector.<BitmapFilterList>;
		
		private var _filtersListChanged:Boolean = false;
		
		private var _animationMatrix:Matrix;
		
		private var _matrixChanged:Boolean = false;
		private var _offsetPoint:Point;
		private var _isComplete:Boolean;
		/**
		 *  实例化一个 DarXBitmapMovieNode
		 * @param darxBitmapDataList 部件动画列表，包含每个需要组合到一起的部件的动画信息
		 * @param matrix 要对动画应用的矩阵变换，用于对动画的缩放，旋转等变换(可选)
		 * @param filtersList 要给列表中每部件单独应用的滤镜列表(可选)
		 * <br/>(暂时只支持发光滤镜，以后会逐步扩大支持种类，使用为被支持的滤镜可能导致缓存错误)
		 */
		public function DarXBitmapAvatarNode(darxBitmapDataList:Vector.<DarXBitmapData>,matrix:Matrix = null,filtersList:Vector.<BitmapFilterList> = null)
		{
			if(darxBitmapDataList != null)
			{
				initDarXBitmapAvatarNode(darxBitmapDataList,matrix,filtersList);
			}
		}
		
		protected function initDarXBitmapAvatarNode(darxBitmapDataList:Vector.<DarXBitmapData>,matrix:Matrix = null,filtersList:Vector.<BitmapFilterList> = null):void
		{
			if(darxBitmapDataList != null)
			{
				if(_darxBitmapDataList && _darxBitmapDataList != darxBitmapDataList)//解除对上一次动画数据的引用
				{	
					DarXBitmapManager.getInstance().reduceCombinQuote(_darxBitmapDataList,_animationMatrix,_filtersList);
				}
				_darxBitmapDataList = darxBitmapDataList;
				_filtersList = filtersList;
				_animationMatrix = matrix;
				DarXBitmapManager.getInstance().drawDarxBitmapDataList(_darxBitmapDataList,onDrawcomplete,_animationMatrix,_filtersList);
			}else{
				_darxBitmapDataList = null;
				_filtersList = null;
				this.stop();
				this.bitmapData = null;
			}
			
		}
		
		protected function onDrawcomplete(frames:Array,offsets:Array = null):void
		{
			this.bitmapData = frames[0];
			initMovieNode(frames,offsets);
			_isComplete = true;
			dispatchEvent(new DarXBitmapMovieNodeEvent(DarXBitmapMovieNodeEvent.DARX_DRAW_COMPLETE));
		}
		
		/**
		 *	更改动画数据
		 * @param darxBitmapDataList 
		 * @param matrix 
		 * @param filtersList 
		 * 
		 */
		public function changeData(darxBitmapDataList:Vector.<DarXBitmapData> = null,matrix:Matrix = null,filtersList:Vector.<BitmapFilterList> = null):void
		{
			if(_darxBitmapDataList != darxBitmapDataList)
			{
				_darxBitmapDataListChanged = true;
			}
			if(_animationMatrix != matrix)
			{
				_matrixChanged = true;
			}
			if(_filtersList != filtersList)
			{
				_filtersListChanged = true;
			}
			
			if(_darxBitmapDataListChanged || _filtersListChanged || _matrixChanged)
			{
				if(_darxBitmapDataList)//解除对上一次动画数据的引用
				{	
					DarXBitmapManager.getInstance().reduceCombinQuote(_darxBitmapDataList,_animationMatrix,_filtersList);
				}
				
				if(_darxBitmapDataListChanged)
				{
					_darxBitmapDataList = darxBitmapDataList;
					_darxBitmapDataListChanged = false;
				}
				if(_matrixChanged)
				{
					_animationMatrix = matrix;
					_matrixChanged = false;
				}
				if(_filtersListChanged)
				{
					_filtersList = filtersList;
					_filtersListChanged = false;
				}
				initDarXBitmapAvatarNode(_darxBitmapDataList,_animationMatrix,_filtersList);
			}
		}
		
		/**
		 * darx动画列表
		 * @return 
		 * 
		 */
		public function get darxBitmapDataList():Vector.<DarXBitmapData>
		{
			return _darxBitmapDataList;
		}
		
		/**
		 *	 动画各部件的滤镜列表 
		 * @return 
		 */
		public function get filtersList():Vector.<BitmapFilterList>
		{
			return _filtersList;
		}

		public function get matrix():Matrix
		{
			return _animationMatrix;
		}
		
		override public function get offsetPoint():Point
		{
			return _offsetPoint;
		}
		
		public function set offsetPoint(value:Point):void
		{
			_offsetPoint = value;
		}

		
		override public function exeRenderNotify():void
		{
			if(!isStop)
			{
				super.exeRenderNotify();
				dispatchEvent(new DarXBitmapMovieNodeEvent(DarXBitmapMovieNodeEvent.DARX_PLAY_COMPLETE));
			}
		}
		
		override public function render():void
		{
			var $lastFrame:int = currentFrame;
			super.render();
			if($lastFrame != currentFrame && currentFrame==totalFrames-1)
			{
				if(totalFrames>1)
				{
					renderNotify = true;//标记渲染完成通知
				}
			}
		}
		
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			super.addEventListener(type, listener, useCapture, priority, useWeakReference);
			if(type == DarXBitmapMovieNodeEvent.DARX_DRAW_COMPLETE)
			{
				if(_isComplete)
				{
					dispatchEvent(new DarXBitmapMovieNodeEvent(DarXBitmapMovieNodeEvent.DARX_DRAW_COMPLETE));
				}
			}
		}
		
		override public function destroy():void
		{
			if(_darxBitmapDataList)//解除引用
			{	
				DarXBitmapManager.getInstance().reduceCombinQuote(_darxBitmapDataList,_animationMatrix,_filtersList);
			}
			_darxBitmapDataList = null;
			_filtersList = null;
			this.stop();
			super.destroy();
		}
		
		override protected function timerEventHandler(event:TimerEvent):void
		{
			//检查动画内容是否发生变化
			super.timerEventHandler(event);
		}
	}
}